package com.snoweagler.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.snoweagler.VO.PageVO;
import com.snoweagler.mapper.MenuMapper;
import com.snoweagler.mapper.RoleMapper;
import com.snoweagler.mapper.RoleMenuMapper;
import com.snoweagler.params.RoleParams;
import com.snoweagler.pojo.*;
import com.snoweagler.service.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    RoleMapper roleMapper;

    @Override
    public PageVO<Role> getUserList(RoleParams params) {
        PageHelper.startPage(params.getCurrentPage(),params.getPageSize());
        List<Role> roleList= roleMapper.findByParams(params);
        PageInfo pi = new PageInfo(roleList);
        PageVO<Role> vo = new PageVO<>(pi.getTotal(),pi.getList());
        return vo;
    }

    @Override
    public void edit(Role role) {
        if(role.getId() != null && !"".equals(role.getId())){
            role.setLastUpdateTime(new Date());

            roleMapper.updateByPrimaryKeySelective(role);
        }else{
            role.setLastUpdateBy(role.getCreateBy());
            role.setLastUpdateTime(new Date());
            roleMapper.insert(role);
        }
    }
    @Resource
    private MenuMapper menuMapper;
    @Override
    public List<Menu> treeDataSelect(Long pid) {
        MenuExample example = new MenuExample();
        example.createCriteria().andParentIdEqualTo(Long.valueOf(pid));
        return menuMapper.selectByExample(example);
    }

    @Resource
    private RoleMenuMapper roleMenuMapper;
    @Override
    public List<Long> getmenuids(Long id) {
        RoleMenuExample roleMenuExample = new RoleMenuExample();
        roleMenuExample.createCriteria().andRoleIdEqualTo(id);
        final List<Long> idList = new ArrayList<>();
        roleMenuMapper.selectByExample(roleMenuExample).forEach(r -> idList.add(r.getMenuId()));

        return idList;
    }


}
